dojo.provide("lotex.dndex");

dojo.require("dojo.io.*");
dojo.require("dojo.html.style");
dojo.require("dojo.dnd.*");
dojo.require("dojo.event.*");

dojo.lang.extend(dojo.dnd.HtmlDragObject, {
    dragClass: "dragClass",
    /**
     * If the drag operation returned a success we reomve the clone of
     * ourself from the original position. If the drag operation returned
     * failure we slide back over to where we came from and end the operation
     * with a little grace.
     */
    onDragEnd: function(e){
        switch(e.dragStatus){

            case "dropSuccess":
                dojo.dom.removeNode(this.dragClone);
                dojo.debugShallow(e.dragObject);
                this.dragClone = null;
                dojo.debug("dropSuccess!");
                break;

            case "dropFailure": // slide back to the start
                var startCoords = dojo.html.getAbsolutePosition(this.dragClone, true);
                // offset the end so the effect can be seen
                var endCoords = [this.dragStartPosition.x + 1,
                    this.dragStartPosition.y + 1];

                // animate
                var line = new dojo.lfx.Line(startCoords, endCoords);
                var anim = new dojo.lfx.Animation(500, line, dojo.lfx.easeOut);
                var dragObject = this;
                dojo.event.connect(anim, "onAnimate", function(e) {
                    dragObject.dragClone.style.left = e[0] + "px";
                    dragObject.dragClone.style.top = e[1] + "px";
                });
                dojo.event.connect(anim, "onEnd", function (e) {
                    // pause for a second (not literally) and disappear
                    dojo.lang.setTimeout(function() {
                            dojo.dom.removeNode(dragObject.dragClone);
                            // Allow drag clone to be gc'ed
                            dragObject.dragClone = null;
                        },
                        200);
                });
                anim.play();
                dojo.debug("dropFailure!");
                break;
        }

        dojo.event.topic.publish('dragEnd', { source: this } );
    }
});

dojo.lang.extend(dojo.dnd.HtmlDropTarget, {
    createDropIndicator: function(){
        this.dropIndicator = document.createElement("div");
        with (this.dropIndicator.style) {
            position = "absolute";
            zIndex = 999;
            borderLeftWidth = "1px";
            borderLeftColor = "red";
            borderLeftStyle = "inset";
            height = dojo.html.getInnerHeight(this.domNode) + "px";
            top = dojo.html.style.getAbsoluteY(this.domNode, true) + "px";
        }
    },

    /**
     * Position the horizontal line that indicates "insert between these two items"
     */
    placeIndicator: function(e, dragObjects, boxIndex, before) {
        with(this.dropIndicator.style){
            //dojo.debug("boxIndex:" + boxIndex + "     before:" + before);
            if (boxIndex < 0) {
                if (this.childBoxes.length) {
                    left = (before ? this.childBoxes[0].left
                        : this.childBoxes[this.childBoxes.length - 1].right) + "px";
                } else {
                    left = dojo.html.getAbsoluteX(this.domNode, true) + "px";
                }
            } else {
                var child = this.childBoxes[boxIndex];
                left = (before ? child.left : child.right) + "px";
            }
        }
    }
});